package com.fu.dynamic;

import com.fu.bean.Student;
import com.fu.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 付大大
 * @description
 * @create 2021-02-01 15:18
 */
public class Test01 {
    @Test
    public void selectCondition() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        Student student = new Student();
        student.setId(2);
        student.setName("李四");
//        student.setAge(24);
        List<Student> list = mapper.selectCondition(student);

        for (Student student1 : list) {
            System.out.println(student1);
        }

        sqlSession.close();
        resourceAsStream.close();
    }


    @Test
    public void selectByIds() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        List<Student> list = mapper.selectByIds(ids);
        for (Student student1 : list) {
            System.out.println(student1);
        }
        sqlSession.close();
        resourceAsStream.close();
    }


}
